package nbp.currency.data;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.persistence.EntityManager;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;

public class HolidayDAOImpl implements HolidayDAO {

	@Override
	public Date getLastWorkingDateBefore(Date date) {

		Session session = new Configuration().configure().buildSessionFactory().openSession();
		Transaction transaction = session.beginTransaction();

		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");

		String queryString = "SELECT get_public_date('" + format.format(date) + "')";
		Query query = session.createSQLQuery(queryString);
		List<Date> result = query.list();
		transaction.commit();
		session.close();

		if (result != null && result.size() == 1) {
			return result.get(0);
		}

		return null;
	}

}
